package it.okkam.longwell.ui.command;

import java.io.*;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;

import edu.mit.simile.longwell.LongwellServer;
import edu.mit.simile.longwell.corpus.TDBConnector;
import edu.mit.simile.longwell.login.CheckLogin;
import edu.mit.simile.longwell.login.LoginUtility;
import edu.mit.simile.longwell.ui.FlairMessage;
import edu.mit.simile.longwell.ui.FlairURL;
import edu.mit.simile.longwell.ui.InjectionManager;
import edu.mit.simile.longwell.ui.command.CommandBase;



public class LogoutCommand extends CommandBase {
	final static private Logger s_logger = Logger.getLogger(LogoutCommand.class);
	CheckLogin check=null;
	public static boolean userInput = false;
	VelocityContext vcContext = null;

	/**
	 * @param injectionManager
	 */
	public LogoutCommand(InjectionManager injectionManager) {
		super(injectionManager);
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.flair.IFlairCommand#execute(edu.mit.simile.flair.FlairMessage)
	 */
	public void execute(FlairMessage msg) throws ServletException {	
		HttpSession userSession = msg.m_request.getSession();
		 s_logger.info("********************The user " +msg.getProfile(userSession.getId()).getUserName() +" has done the logout************************");
		 
		//save the logout into the triplestore with name and date
		LoginUtility.saveLogout(msg.getProfile(userSession.getId()).getUserName());
		LongwellServer.getServer().removeUserName(userSession.getId());
		LongwellServer.getServer().dispose(userSession.getId());
        for(Enumeration e = userSession.getAttributeNames(); e.hasMoreElements(); ){
            String attName = (String)e.nextElement();
            userSession.removeAttribute(attName);
        }	
       
		userSession.invalidate();
		System.gc();
		try {		
			msg.m_response.sendRedirect(new FlairURL(msg).changeCommandQuery("home","").toURLString());
			//msg=null;
			//System.gc();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}	
